'''
@Author: CaiJianhao
@Date: 2020-01-20 08:47:09
@Descripttion: 
@LastEditTime : 2020-01-20 13:40:26
@LastEditors  : CaiJianhao
@version: 
'''
import pytesseract
from PIL import Image

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
tessdata_dir_config = '--tessdata-dir "C:/Program Files/Tesseract-OCR/tessdata"'

image = Image.open("img\sc.jpg")


Img = image.convert('L')
Img.save("test1.jpg")
 
# 自定义灰度界限，大于这个值为黑色，小于这个值为白色
threshold = 200
 
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)
 
# 图片二值化
photo = Img.point(table, '1')

img2 =  Image.open("test1.jpg")

code = pytesseract.image_to_string(img2, config=tessdata_dir_config)
text = pytesseract.image_to_string((img2), lang='chi_sim')
print(code)
print(text)